<?php
    class donvitinhdb
    {
        public $_tablename = "giaphucvn_donvitinh";
        public function getList($MaDonViTinh = 0, $TenDonViTinh = '', $order = '', $start = 0, $num = 30, $all = 0)
		{
			$sql = "SELECT * FROM ".$this->_tablename." WHERE (1)";
			
			if ($MaDonViTinh != 0)
				$sql .= " AND MaDonViTinh = " . dataservice::QuoteSmart($MaDonViTinh);
			
			if ($TenDonViTinh != '')
				$sql .= " AND TenDonViTinh LIKE " . dataservice::QuoteSmart('%' . $TenDonViTinh . '%');
			
			if ($order != '')
				$sql .= " ORDER BY " . $order . " DESC";
			
			if($all == 0)
				$sql .= " LIMIT " . $start . ", ".$num;
			//echo $sql;
			$db = new dataservice();
			$ret = $db->ExecuteQuery($sql);
			return $ret;
		}
        
        public function add($TenDonViTinh = '')
		{
			$sql = "INSERT INTO ".$this->_tablename."(TenDonViTinh) VALUES(" . dataservice::QuoteSmart($TenDonViTinh) . ")";
			
			$db = new dataservice();
			$ret = $db->ExecuteNonQuery($sql);
			return $ret;
		}
		
		public function update($MaDonViTinh = 0, $TenDonViTinh = '')
		{
			$sql = "UPDATE ".$this->_tablename." SET TenDonViTinh = " . dataservice::QuoteSmart($TenDonViTinh) . " WHERE MaDonViTinh = ".dataservice::QuoteSmart($MaDonViTinh);
			//echo $sql;
			$db = new dataservice();
			$ret = $db->ExecuteNonQuery($sql);
			return $ret;
		}
		
		public function delete($MaDonViTinh = 0)
		{
			$sql = "DELETE FROM ".$this->_tablename." WHERE (1)";
			
			if ($MaDonViTinh != 0)
				$sql .= " AND MaDonViTinh = " . dataservice::QuoteSmart($MaDonViTinh);
			
			$db = new dataservice();
			return $db->ExecuteNonQuery($sql);
		}
        
        public function issetTypes($MaDonViTinh = 0, $TenDonViTinh = "")
		{
			$sql = "SELECT COUNT(*) AS SoDong FROM ".$this->_tablename." WHERE (1)";
            
            if($MaDonViTinh != 0)
				$sql .= " AND MaDonViTinh = $MaDonViTinh";
            
			if($TenDonViTinh != "")
				$sql .= " OR TenDonViTinh = '$TenDonViTinh'";
			
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
        
        public function issetType($TenDonViTinh = "")
		{
			$sql = "SELECT COUNT(*) AS SoDong FROM ".$this->_tablename." WHERE (1)";
			if($TenDonViTinh != "")
				$sql .= " AND TenDonViTinh = '$TenDonViTinh'";
			
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
    }
?>